package org.csu.mypetstore.persistence;

import java.sql.*;

public class DBUtil {
    private static final String url = "jdbc:mysql://127.0.0.1:3306/mypetstore?useSSL=false";
    private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
    private static final String username = "root";
    private static final String password = "123456";

    public static Connection getConnection () throws Exception{
        Class.forName(DRIVER_CLASS);
        Connection connection = DriverManager.getConnection(url,username,password);
        return connection;
    }
    public static void closeConnection(Connection connection)throws Exception{
        if(connection != null){
            connection.close();
        }
    }

    public static void closeStatement(Statement statement)throws Exception{
        if(statement != null){
            statement.close();
        }
    }

    public static void closePreparedStatement(PreparedStatement preparedStatement)throws Exception{
        if(preparedStatement != null){
            preparedStatement.close();
        }
    }

    public static void closeResultSet(ResultSet resultSet)throws Exception{
        if(resultSet != null){
            resultSet.close();
        }
    }

    //test
//    public static void main(String[] args) throws Exception {
//        Connection connection = DBUtil.getConnection();
//        System.out.println(connection);
//    }
}
